//
//  ProjectDetailViewController.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-25.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "ProjectDetailViewController.h"
#import "ProjectDetailTopView.h"
#import "CustomProgressView.h"
#import "InvestView.h"
#import "UIScrollView+extension.h"

@interface ProjectDetailViewController ()

@property (nonatomic,strong)CustomProgressView * progressView;
@property (nonatomic,strong)ProjectDetailTopView * topView;
@property (nonatomic,strong)InvestView * bottomView;
@property (nonatomic,strong)UIScrollView * scrollView;

@end

@implementation ProjectDetailViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hideTabBar:YES];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"xbj01.png"] forBarMetrics:UIBarMetricsDefault];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self hideTabBar:NO];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"123.png"] forBarMetrics:UIBarMetricsDefault];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self uiConfig];
    [self hideTabBar:YES];
    // Do any additional setup after loading the view.
}
#pragma mark - 搭建UI
-(void)uiConfig{
    self.view.backgroundColor = [UIColor whiteColor];
    //设置navigation
    [self setNavigationBarTitle:@"项目详情"];
    [self setNavigationLeftBarWithTitle:nil withNormalImageName:@"back.png" withSelectedImageName:nil withAction:@selector(backBtnClick:)];
    [self setNavigationRightBarWithTitle:nil withNormalImageName:@"share.png" withSelectedImageName:nil withAction:@selector(shareBtnClick:)];
    
    //导航栏底部的线
    self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"401line.png"];
    //self.navigationController.navigationBar.shadowImage = [[UIImage alloc]init];
    
    _topView = [[[NSBundle mainBundle]loadNibNamed:@"ProjectDetailTopView" owner:self options:nil]lastObject];
    [_topView createProgressView:0.8];
    
    _bottomView = [[[NSBundle mainBundle]loadNibNamed:@"InvestView" owner:self options:nil]lastObject];
    _bottomView.frame = CGRectMake(0, _topView.frame.size.height, __MainScreen_Width, 270);
    _bottomView.investAmountTF.frame = CGRectMake(13, 158, 299, 45);
    _bottomView.investAmountTF.delegate =self;
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height)];
    _scrollView.contentSize = CGSizeMake(__MainScreen_Width, _topView.frame.size.height+_bottomView.frame.size.height+16);
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    [_scrollView addSubview:_topView];
    [_scrollView addSubview:_bottomView];
}
#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == _bottomView.investAmountTF) {
        [UIView animateWithDuration:0.3 animations:^{
            _scrollView.contentOffset = CGPointMake(0, _bottomView.frame.size.height+200);
            
        }];
        return YES;
    }
    return NO;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.3 animations:^{
        _scrollView.contentOffset = CGPointMake(0, _scrollView.contentSize.height - __MainScreen_Height+40);
    }];
    

}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //设置scrollView的上下偏移的界限 往下拉为正。
    [UIScrollView setScrollAreaWithScrollView:scrollView withFrontLocation:300 withBottomLocation:-200];
}


#pragma mark - Touch事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_bottomView.investAmountTF resignFirstResponder];
    _scrollView.contentOffset = CGPointMake(0, _scrollView.contentSize.height - __MainScreen_Height+40);

}

#pragma mark - 导航栏点击事件
-(void)shareBtnClick:(UIButton *)btn{
    NSLog(@"share");
}

-(void)backBtnClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    //[self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    self.progressView = nil;
    self.topView = nil;
    self.bottomView = nil;
    self.scrollView = nil;
    // Dispose of any resources that can be recreated.
}

@end
